/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import Entity.TheLoai;
import Util.DatabaseHandle;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author copang
 */
public class TheLoaiImpl implements TheLoaiDAO{

    public boolean add(TheLoai data) {
        String sql = "{ call sp_THELOAI_add(?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setOutParameter(1, java.sql.Types.INTEGER);
            db.setParameter(2, data.getTenTheLoai());
            db.setParameter(3, data.getDienGiaiTL());

            flag = db.executeStorePro();
            if (flag)
                data.setMaTheLoai(db.getIntOutValue(1));
            
            db.closed();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(TheLoaiImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean delete(int MaTheLoai) {
        String sql = "{ call sp_THELOAI_delete(?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {

            db.setParameter(1, MaTheLoai);
            flag = db.executeStorePro();
            db.closed();
            return flag;
        } catch (SQLException ex) {
            Logger.getLogger(TheLoaiImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public boolean edit(TheLoai data) {
        String sql = "{ call sp_THELOAI_edit(?,?,?) }";
        DatabaseHandle db = new DatabaseHandle(sql, true);
        boolean flag;
        try {
            db.setParameter(1, data.getMaTheLoai());
            db.setParameter(2, data.getTenTheLoai());
            db.setParameter(3, data.getDienGiaiTL());

            flag = db.executeStorePro();
            db.closed();
            return flag;
        } catch (SQLException ex) {
            Logger.getLogger(TheLoaiImpl.class.getName()).log(Level.SEVERE, null, ex);
            db.closed();
            return false;
        }
    }

    public ResultSet getAll() {
        String sql = "{call sp_THELOAI_getAll()}";
        DatabaseHandle db = new DatabaseHandle(sql, false);
        ResultSet rs = db.excuteQuery();
        return rs;
    }

}
